package com.viewpagertext.service;

import android.app.Activity;
import android.app.ActivityManager;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;

import java.util.List;

public class NotificationClickReceiver extends BroadcastReceiver {

    public static final String TAG = "NotificationClickReceiver";

    public void getCurrentActivityClass(Context context) {
        if (context instanceof Activity) {
            Class<?> currentActivityClass = context.getClass();
            // 使用currentActivityClass做你需要做的事情
        }
    }

    @Override
    public void onReceive(Context context, Intent intent) {


        System.out.println("通知栏点击");
        ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        List<ActivityManager.RunningTaskInfo> tasks = am.getRunningTasks(1);
        ComponentName topActivity = tasks.get(0).topActivity;
        String className = topActivity.getClassName();
        //获取栈顶的Activity
//        Activity currentActivity = ActivityManager.getCurrentActivity();
        intent = new Intent(Intent.ACTION_MAIN);
        intent.setComponent(topActivity);
        intent.addCategory(Intent.CATEGORY_LAUNCHER);
        try {
            intent.setClass(context, Class.forName(className));
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
        context.startActivity(intent);

    }
}